﻿using System;
using GameEngine.Model;
using GameEngine.Model.Data;
using GameEngine.View;
using GameEngine.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using System.Collections.Generic;

namespace GameEngine.Controller
{
    public class CreateLevelCommand<MyIdType, MyBaseUIElementType> : SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            ILevelVO<MyIdType> levelVO = notification.Body as ILevelVO<MyIdType>;

            ILevelDataProxy<MyIdType> level = ModelFacade<MyIdType>.Instance.CreateLevelDataProxy(levelVO);
            ApplicationFacade.Instance.RegisterProxy(level);

            IDictionary<IWorldVOId,WorldMediator<MyIdType, MyBaseUIElementType>> worldMeds = new Dictionary<IWorldVOId,WorldMediator<MyIdType, MyBaseUIElementType>>();
            foreach (IWorldVO<MyIdType> world in level.VO.Worlds)
            {
                ApplicationFacadeHelper<MyIdType>.CreateWorld(world);
                level.Worlds.Add(
                    world.Id,
                    ModelFacade<MyIdType>.Helper.GetWorldDataProxy(world));
                worldMeds.Add(world.Id,ViewFacade<MyIdType, MyBaseUIElementType>.Helper.GetWorldMediator(world.Id));
            }

            ApplicationFacade.Instance.RegisterMediator(
                ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateLevelMediator(
                    level,
                    worldMeds,
                    ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateLevelView()));
        }
    }
}
